<template>
  <div>
    <!-- 面包屑导航栏区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="10">
          <!-- 搜索框区域 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="210px"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status==='1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="160px">
          <template slot-scope="scope">{{scope.row.create_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showSiteDialog"></el-button>
            <el-button
              type="danger"
              icon="el-icon-location"
              size="mini"
              @click="showLogisticsDialog(scope.row.order_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑收货地址对话框 -->
    <el-dialog title="收货地址" :visible.sync="siteDialogVisible" width="50%" @close="resetSiteForm">
      <el-form ref="siteFormRef" :model="siteForm" label-width="100px" :rules="siteFormrules">
        <el-form-item label="省市区/县：" prop="site1">
          <el-cascader
            v-model="siteForm.site1"
            :options="cascaderData"
            :props="cascaderProps"
            style="width:100%;"
            clearable
          ></el-cascader>
          <!-- <el-input v-model="siteForm.site1"></el-input> -->
        </el-form-item>
        <el-form-item label="详细地址：" prop="site2">
          <el-input v-model="siteForm.site2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="siteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="siteDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑收货地址对话框 -->
    <el-dialog title="物流信息" :visible.sync="lodisticsDialogVisible" width="50%">
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 导入城市数据
import cityData from '../../assets/js/citydata'
export default {
  data() {
    return {
      // 存储获取订单列表的请求参数
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示数据条数
        pagesize: 10
      },
      // 存储订单列表
      orderList: [],
      // 存储订单总数量
      total: 0,
      // 控制收货地址对话框是否可见
      siteDialogVisible: false,
      // 存储收货地址的表单信息
      siteForm: {
        site1: [],
        site2: ''
      },
      // 收货地址的表单验证规则
      siteFormrules: {
        site1: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        site2: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      // 级联选择器的配置
      cascaderProps: { expandTrigger: 'hover' },
      // 级联选择器的数据来源
      cascaderData: cityData,
      // 控制物流信息对话框是否可见
      lodisticsDialogVisible: false,
      // 存储物流信息
      activities: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表
    async getOrderList() {
      const res = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      // 获取订单列表成功
      this.$message.success('获取订单列表成功')
      // 保存订单列表信息
      this.orderList = res.data.goods
      // 保存总共订单数量
      this.total = res.data.total
    },
    // 当页面呈现的数据条数变化时触发
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      // 刷新页面
      this.getOrderList()
    },
    // 当页码变化时触发
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      // 刷新页面
      this.getOrderList()
    },
    // 渲染编辑地址对话框
    showSiteDialog() {
      // 打开对话框
      this.siteDialogVisible = true
    },
    // 重置地址对话框中的表单
    resetSiteForm() {
      this.$refs.siteFormRef.resetFields()
    },
    // 渲染物流信息对话框
    showLogisticsDialog(id) {
      // 获取物流信息,供测试的物流单号：1106975712662
      this.getLogistics(1106975712662)
      // 打开对话框
      this.lodisticsDialogVisible = true
    },
    // 获得物流信息
    async getLogistics(id) {
      const res = await this.$http.get(`/kuaidi/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败')
      }
      // 获取物流信息成功
      this.$message.success('获取物流信息成功')
      this.activities = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  margin: 0;
}
</style>